<?php
/**
 * Patient Signal form for updating signal files. Used in updateFile action in PatientSignalController.
 *
 * @package   views
 * @subpackage patientSignal
 * @since     1.0
 * @filesource
 */

$this->breadcrumbs=array(
	'Patient Signals'=>array('index'),
	$model->subject=>array('view','id'=>$model->id),
	'Update Files',
);
?>

<h1>Update Signal Files</h1>

<div class="clear"></div>
<div class="form">
<?php
Yii::app()->clientScript->registerCoreScript('jquery');

$deleteImage=CHtml::image(Yii::app()->baseUrl.'/images/icons/cross.png','Delete');
$this->widget('ext.plupload.PluploadWidget', array(
	'config' => array(
		//'runtimes' => 'html5',
		'url' => $this->createUrl('patientSignal/uploadFilesPlupload'),
		//'max_file_size' => str_replace("M", "mb", ini_get('upload_max_filesize')),
		'max_file_size' => '100mb',
		'chunk_size' => '1mb',
		'unique_names' => true,
		'filters' => array(array('title' => 'SMR Files', 'extensions' => 'smr'),),
		//'browse_button'=>'uploadButton',
		'language' => 'en-smr',
		'max_file_number' => 6,
		'autostart' => false,
		'jquery_ui' => true,
		'browse_button'=>'uploader_browse',
		'reset_after_upload' => false,
		//using this flag to disable CSRF validation for uploads
		'headers'=>array('XUPLOAD'=>"true"),
	),
	'callbacks' => array(
		'FileUploaded' => 'function(up,file,response){
		up.refresh();
		jQuery.post( "'.$this->createUrl('patientSignal/updateFilesSave',array('signalId'=>$model->id)).'", {id:file.id,name:file.name,'.Yii::app()->request->csrfTokenName.':"'.Yii::app()->request->csrfToken.'"}, 
		function(data, textStatus, jqXHR){
			var fileContainer=$("#signal-files-list");
			if(data.success){
				var newLine=$("<li></li>")
					.addClass("signal-file-item").appendTo(fileContainer);
				var filename=$("<span></span>")
							.html(data.filename)
							.appendTo(newLine);
				var image=$("<a></a>")
							.attr("href",data.deleteUrl)
							.addClass("signal-delete")
							.html(\''.$deleteImage.'\')
							.appendTo(newLine);
			}else{
				var message=data.message || "Unknown Error happened when processing file.";
				var errorMessage=$("<li></li>")
					.css("color","#FF0000")
					.appendTo(fileContainer).addClass("errorMessage").html(message);
				window.setTimeout(function(){
					errorMessage.fadeOut(2000);
				},4000);
			}
		},"json" );
		}',
		),
	'id' => 'uploader'
	)
);
?>

<?php $form=$this->beginWidget('CActiveForm', array(
	'id'=>'patient-signal-upload6-upload6-form',
	'enableAjaxValidation'=>FALSE,
)); ?>

	
	<?php echo $form->errorSummary($model); ?>
<fieldset>
	<legend>Uploaded Files</legend>
<ul id="signal-files-list" class="signal-files-list">
<?php
$signalFiles=$model->signalFiles;
foreach ($signalFiles as $signalFile){
	?>
	<li class="signal-file-item"><span><?php echo $signalFile->filename; ?> </span><?php echo CHtml::link(
		CHtml::image(Yii::app()->baseUrl.'/images/icons/cross.png','Delete')
	,array('patientSignal/deleteSignalFile','fileId'=>$signalFile->id),
	array('class'=>'signal-delete'))?></li>
	<?php
}
Yii::app()->clientScript->registerScript('signal-delete',<<<EOF
jQuery(".signal-delete").live('click',function(){
	// at least one file should be there.
	var fileCount=$('.signal-file-item').length;
	console.log(fileCount);
	if(fileCount<=1){
		alert('At least ONE signal file needs to be kept!');
		return false;
	}
	return confirm("Delete the signal file? (File will be permanently deleted from server!)");
});
EOF
)
?>
</ul>
</fieldset>
<?php $this->endWidget(); ?>
</div><!-- form -->
